import pandas as pd
import numpy as np
import re

###  1. Series数据结构
# s = pd.Series(data = [1,2],index = [3,3])
# print(s[3])


## 数据为标量，索引为[1,2,3]，会按照索引的数目用该标量补充
# s1 = pd.Series(66, index = [1,2,3])
# print("数据为标量，索引为[1,2,3]，会按照索引的数目用该标量补充:\n",s1)
#
# print("--------------")
# s2 = pd.Series(66)
# print("数据为标量，设置index，默认状态时生成只有一组数据:\n",s2)

## 数组或列表创建Series，index默认，索引默认从0开始

# s1 = pd.Series(data = np.arange(5,10))
# print("数组或列表创建Series，index默认，索引默认从0开始:\n",s1)
#
# print("--------------")
# s2 = pd.Series(data = np.arange(1,6),index = ['A','B','C','D',"E"], name = "自然数", dtype = "float32")
# print("自定义索引、数据类型，命名为自然数：\n",s2)

## 字典创建Series

# dic = {'b':6, 'c':3, 'a':2, 'w': 8}
# s1 = pd.Series(data =dic )
# print("字典创建Series,index默认：\n",s1)
#
# print("--------------")
# s2 = pd.Series(data = dic,index = ['a','b','c','d'])
# print("字典创建Series，index为 ['A','B','C','D']：\n",s2)
# print("注：字典中的key和标签不匹配就不显示，多出的标签填空值")

## Series的操作

##  Series 的属性
# s = pd.Series(data = np.arange(1,7),index = np.arange(1,7),name = "学习", dtype = "int32")
# print("Series:\n",s)
# print("------------")
# print("Serise的属性values：",s.values)
# print("Serise的属性index：",s.index)
# print("Serise的属性name：", s.name)
# print("Serise的属性dtype：", s.dtype)

# # ## Series支持数组类似的操作
# dic = {'b':6, 'c':3, 'a':2, 'w': 8}
# s1 = pd.Series(data =dic )
# print("Series:\n",s1)
# print("用索引a，s1['a']:", s1['a'])
#
# print("------------")
# s2 = pd.Series(data = np.arange(5,10),index = np.arange(1,6))
# print("Series:\n",s2)
# print("用索引1，实际打印出的是标签对应的数，s2[1]:", s2[1])
#
# print("------------")
# s3 = pd.Series(data = [1,2,3,4], index = [6,8,6,8])
# print("用索引6,实际打印的是标签全为6 index和value :\n",s3[6])

#
# # print("------------")
# s2 = pd.Series(data = np.arange(5,10),index = np.arange(1,6))
# print("Series:\n",s2)
# print("用切片 s2[2:5] ,位置2-位置4对应的 index和value ：\n",s2[2:5])
# print("用切片 s2[0:1] ，位置0对应的 index和value ：\n",s2[0:1])

# ## 适用字典的操作
# s = pd.Series(data = ['a','b','c','d','e'],index = np.arange(1,6))
# print("Series:\n",s)
# print( "索引标签 1, 'b' in s：", 1 in s)
# print( "索引标签 9, 'z' in s：", 9 in s)
# print("索引标签 1 ，s.get(1)，直接返回对应的值：", s.get(1) )
# print("索引标签‘j' ，s.get('j')，每该标签返回值为Nan：", s.get('a') )

# ## 两个Series相加
#
# s1 = pd.Series(data = np.arange(1,6))
# s2 = pd.Series(data = np.arange(9,4,-1))
# print("Series:\n",s1)
# print("Series:\n",s2)
# #加减乘车类似
# print("s1 + s2:\n",s1+s2)

#### DateFrame

## 默认索引和自定义索引
# data = np.arange(0,9).reshape(3,3)
# df1 = pd.DataFrame(data = data)
# print("默认行索引和列索引：\n",df1)
# print("------------")
# df2 = pd.DataFrame(data = data,index = ['a','b','c'],columns = ['A','B','C'])
# print("自定义行索引和列索引：\n",df2)

# # ## 字典类型
# #
# dic = { "name":['Tom','jacker','dog'], "age":[18, 19, 18], "number":[111,222,333]}
# df = pd.DataFrame(data = dic)
# print(df)
# print("------------")
# print("df[]只能索引一个列的标签：\n",df["name"])
# print("------------")
# print("df.loc[行的标签][列的标签]索引某个值：",df.loc[0]['name'])
# print("------------")
# print("df.loc[]只能索引一个行的标签：\n:",df.loc[1])
# print("------------")
# print("df.loc[[行标签的列表]]索引行的标签：\n:",df.loc[[1, 2]])
# print("------------")
# print("df.loc[:,[列标签的列表]]索引m列的标签：\n:",df.loc[:,["name", "age"]])
# print("------------")
# print("df.loc[[行标签的列表][列标签的列表]]索引n行m列：\n",df.loc[[1,2],['name','number']])
# print("------------")
# print("df.iloc[行标签的位置][列标签的位置]索引n行m列，：\n",df.iloc[[1,2],[1,2]])

## 加入条件
# dic = { "name":['Tom','jacker','dog'], "age":[18, 19, 18], "number":[111,222,333]}
# df = pd.DataFrame(data = dic)
#
# data1 = df.loc[:,"name":"number"]
# print("使用切片 'name':'number' ：\n",data1)
# print("-----------")
# data2 = df.loc[df['age']<19,"name":"number"]
# print('使用条件和切片 .loc[df["age"]<19,"name":"number"]：\n',data2)

# l = [0,1,2,3]
# print((l[1:3]))

## Series 和 DataFrame 的联系
# s1 = pd.Series(data = np.arange(1,4),name = "序列")
# s2 = pd.Series(data = ["李明","李华", "小明"], name = "name")
# df = pd.DataFrame(data = {s1.name:s1.values, s2.name: s2.values})
# print("打印 s1 ：\n",s1)
# print("打印 s2 ：\n",s2)
# print("打印 s1和s2 的组合转换 :\n",df)

# s1 = pd.Series(data = np.arange(1,4), index = [1,2,3],name = "序列")
# s2 = pd.Series(data = ["李明","李华", "小明"],index = [1,2,3] ,name = "name")
# df1 = pd.DataFrame(data = {s1.name:s1, s2.name: s2})
# print("打印 s1 ：\n",s1)
# print("打印 s2 ：\n",s2)
# print("s1和s2的索引都一致:\n",df1)
# print("------------")
#
# s3 = pd.Series(data = np.arange(1,4), index = [1,2,3],name = "序列")
# s4 = pd.Series(data = ["李明","李华", "小明"],index = [2,3,4] ,name = "name")
# df2 = pd.DataFrame(data = {s3.name:s3, s4.name: s4})
# print("打印 s3 ：\n",s3)
# print("打印 s4 ：\n",s4)
# print("s3和s4的索引不一致:\n",df2)

### 写入文件
## 写入csv 文件

# data = {
#     'Name': ['Alice', 'Bob', 'Charlie'],
#     'Age': [25, 30, 35],
#     'City': ['北京', '上海', '广州']
# }
#
# df = pd.DataFrame(data)
# # # 写入 JSON 文件
# df.to_json('output.json', orient='records', force_ascii=False)




## 读取csv（txt）文件
# df = pd.read_csv("test.txt",encoding = "utf-8")
# print(df)
# df = pd.read_table("test.txt",sep = "\s+,|\s+|,", encoding = "utf-8",engine='python')
# print(df)

## 改时间戳

# df = pd.read_csv("test.txt",index_col = "birthday")
# print("打印df对象：\n",df)
# print("此时的行索引为：",df.index)
# print("object 类型是整体类型，不是时间戳")
# print("---------")
# print("把整体类型的时间改为时间戳,时间戳是pandas可以直接索引的类型")
# df.index = pd.to_datetime(df.index)
# print(df.index)
# print("---------")
# print("打印df对象：\n",df)
# print("打印df中2003年出生的同学：\n",df.loc["2003"])


## 读取excel文件


# df = pd.read_excel("test.xlsx")
# print(df)

# ## 无表头
# df = pd.read_excel("test.xlsx",header= None, names= ["序列号","姓名", "年龄"])
# print(df)

## 读取json文件

# df = pd.read_json("test.json",encoding = "rb")
# print(df)
#
# s3 = pd.Series(data = np.arange(1,4), index = [1,2,3],name = "序列")
# s4 = pd.Series(data = ["李明","李华", "小明"],index = [2,3,4] ,name = "name")
# df2 = pd.DataFrame(data = {s3.name:s3, s4.name: s4})
#
# pd.to_json("test_1",df2)

# ###  DataFrame数据的增加和删除
#
# s1 = pd.Series(data = np.arange(1,4),name = "序列")
# s2 = pd.Series(data = ["李明","李华", "小明"], name = "name")
# df = pd.DataFrame(data = {s1.name:s1.values, s2.name: s2.values})
# #print("打印df对象：\n",df)
#
# df['test1'] = 66
# #print("增加一列test1：\n", df)
# df['test2'] = pd.Series([77,77,77], index = np.arange(0,3))
# #print("增加一列test2：\n", df)
# df.insert(1,'test3',df['test2'])
# #print('增加一列test3, 在位置1，名为test3，值为df["test2"]：\n', df)
#
# print("打印df对象：\n",df)
# del df['test3']
# print("删除数据test3：\n",df)
# df.pop('test2')
# print("删除数据test2：\n",df)

## 修改数据

# s1 = pd.Series(data = np.arange(1,4),name = "序列")
# s2 = pd.Series(data = ["李明","李华", "小明"], name = "name")
# df = pd.DataFrame(data = {s1.name:s1.values, s2.name: s2.values})
# print("打印df对象：\n",df)
#
# df.loc[0,"name"] = "***"
# print("打印修改后的df对象：\n",df)
